package com.woniu.cms.auth.core.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.woniu.cms.auth.core.entity.RbacRole;
import com.woniu.cms.auth.core.service.IRbacRoleService;
import com.woniu.cms.auth.core.vo.RoleVo;
import com.woniu.common.api.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lilili
 * @since 2022-09-16
 */
@RestController
@RequestMapping("/rbac-role")
@Api(tags="角色控制器")
public class RbacRoleController {

    @Resource
    private IRbacRoleService roleService;

    /**
     * 分配角色时需要展示所有的角色
     *
     * @return {@link Result}<{@link List}<{@link RbacRole}>>
     */
    @GetMapping
    @ApiOperation("获取所有角色")
    public Result<List<RbacRole>> all(){
        List<RbacRole> list = roleService.list();
        return Result.common(list);
    }


    @GetMapping("/{userId}")
    @ApiOperation("获取对应用户的角色")
    public Result<List<RbacRole>> allByUserId(@PathVariable("userId") Integer userId){
        List<RbacRole> list = roleService.allByUserId(userId);
        return Result.common(list);
    }

    /**
     * 分配角色时展示数据
     * 需要返回所有已分配角色和未分配角色数组。
     * 定义RoleVo对象包含两个数组
     *
     * @return {@link Result}<{@link List}<{@link RbacRole}>>
     */
    @GetMapping("/display/{userId}")
    @ApiOperation("展示用户已分配角色和未分配角色")
    public Result<RoleVo> display(@PathVariable("userId") Integer userId){
        RoleVo roleVo = roleService.display(userId);
        return Result.common(roleVo);
    }


    /**
     * 分配
     *  假设有两个框，左边显示未分配角色，右边展示已分配角色。可以单选可以全选，从左框到右框则分配角色
     *
     * @param userId  用户id
     * @param roleIds 角色id
     * @return {@link Result}<{@link Object}>
     */
    @PostMapping("/doAllot}/{userId}")
    @ApiOperation("分配用户角色")
    public Result<Object> doAllot(@PathVariable("userId") Integer userId, @RequestBody Integer[] roleIds){
        boolean add = roleService.doAllot(userId, roleIds);
        return Result.common(add);
    }

    @DeleteMapping("/doUnAllot}/{userId}")
    @ApiOperation("删除用户角色")
    public Result<Object> doUnAllot(@PathVariable("userId") Integer userId, @RequestBody Integer[] roleIds){
        boolean delete = roleService.doUnAllot(userId, roleIds);
        return Result.common(delete);
    }
}

